//
//  YCCustomAligmentBtn.m
//  YouCabService
//
//  Created by gml on 2018/11/29.
//  Copyright © 2018年 NQ. All rights reserved.
//

#import "YCCustomAligmentBtn.h"

@implementation YCCustomAligmentBtn

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat space = self.spaceBetweenTitleAndImage;
    
    //titleLabel的宽度
    CGFloat titleW = CGRectGetWidth(self.titleLabel.bounds);
    //titleLabel的高度
    CGFloat titleH = CGRectGetHeight(self.titleLabel.bounds);
    
    //imageView的宽度
    CGFloat imageW = CGRectGetWidth(self.imageView.bounds);
    //imageView的高度
    CGFloat imageH = CGRectGetHeight(self.imageView.bounds);
    
    //按钮中心点X的坐标（以按钮左上角为原点的坐标系）
    CGFloat btnCenterX = CGRectGetWidth(self.bounds) / 2;
    //imageView中心点X的坐标（以按钮左上角为原点的坐标系）
    CGFloat imageCenterX = btnCenterX - titleW / 2;
    //titleLabel中心点X的坐标（以按钮左上角为原点的坐标系）
    CGFloat titleCenterX = btnCenterX + imageW / 2;
    
    switch (self.imageAlignment)
    {
        case BtnImageAlignmentTopType:
        {
            self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            //文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
            [self setTitleEdgeInsets:UIEdgeInsetsMake(self.imageView.frame.size.height+10 ,-self.imageView.frame.size.width, 0.0,0.0)];
            //图片距离右边框距离减少图片的宽度，其它不边
            [self setImageEdgeInsets:UIEdgeInsetsMake(-10, 0.0,0.0, -self.titleLabel.bounds.size.width)];
//            self.titleEdgeInsets = UIEdgeInsetsMake(imageH / 2 + space / 2, -(titleCenterX - btnCenterX), -imageH / 2, titleCenterX - btnCenterX);
//            self.imageEdgeInsets = UIEdgeInsetsMake(-(titleH / 2 + space / 2), btnCenterX - imageCenterX, titleH / 2+ space / 2, -(btnCenterX - imageCenterX));
        }
            break;
        case BtnImageAlignmentLeftType:
        {
            self.titleEdgeInsets = UIEdgeInsetsMake(0, space / 2, 0,  -space / 2);
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -space / 2, 0, space);
        }
            break;
        case BtnImageAlignmentBottomType:
        {
            self.titleEdgeInsets = UIEdgeInsetsMake(-(imageH / 2+ space / 2), -(titleCenterX - btnCenterX), imageH / 2 + space / 2, titleCenterX - btnCenterX);
            self.imageEdgeInsets = UIEdgeInsetsMake(titleH / 2 + space / 2, btnCenterX - imageCenterX,-(titleH / 2+ space / 2), -(btnCenterX - imageCenterX));
        }
            break;
        case BtnImageAlignmentRightType:
        {
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageW + space / 2), 0, imageW + space / 2);
            self.imageEdgeInsets = UIEdgeInsetsMake(0, titleW+space / 2, 0, -(titleW+space / 2));
        }
            break;
    }
}
@end
